package com.jiao.store.controller;

import com.jiao.store.entity.Address;
import com.jiao.store.service.AddressService;
import com.jiao.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("addresses")
@RestController
public class AddressController extends  BaseController{
    @Autowired
    private AddressService addressService;


    @RequestMapping("addNewAddress")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session){
        Integer uid=getUidSession(session);
        String username=getUserNameFromSession(session);
            addressService.AddNewAddress(uid,username,address);
        System.out.println("uid:"+uid);
        System.out.println("username:"+username);
        System.out.println("address:"+address);
        return new JsonResult<>(Ok);
    }

     @RequestMapping({"","/"})
    public  JsonResult<List<Address>> getByUid(HttpSession session){
          Integer  uid=getUidSession(session);
         List<Address> data = addressService.getByUid(uid);
         return  new JsonResult<>(Ok,data);
    }


    //RestFul风格请求编写
    @RequestMapping("{aid}/setDefault")
    public  JsonResult<Void> setDefault(@PathVariable("aid")Integer aid,  HttpSession session){
            addressService.setDefault(aid,getUidSession(session),getUserNameFromSession(session));
        return  new JsonResult<>(Ok);
    }


    @RequestMapping("{aid}/delete")
    public  JsonResult<Void>  delete(@PathVariable("aid")Integer aid,HttpSession session){
        addressService.delByAid(aid,getUidSession(session),getUserNameFromSession(session));
        return  new JsonResult<>(Ok);
    }
//    @RequestMapping("{aid}/updateAddress")
//    public  JsonResult<Void> updateAddress(@PathVariable("aid")Integer aid,HttpSession session){
//        Address address=new Address();
//        address.setAid(aid);
//        address.setModifiedUser(getUserNameFromSession(session));
//        address.setModifiedTime(new Date());
//        addressService.updateAddress(address);
//        return  new JsonResult<>(Ok);
//
//    }
}
